package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealName;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserService;
import com.heima.user.service.UserAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class UserAuditServiceImpl extends ServiceImpl<ApUserRealnameMapper,ApUserRealName> implements UserAuditService {

    /**
     * 获取审核列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userlist(AuthDto dto) {
        //1.校验参数
        dto.checkParam();
        //分页条件查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealName> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //通过状态条件查询
        if(dto.getStatus() != null){
             lambdaQueryWrapper.eq(ApUserRealName::getStatus, dto.getStatus());
        }
        //排序
        lambdaQueryWrapper.orderByDesc(ApUserRealName::getCreatedTime);
        page = page(page,lambdaQueryWrapper);
        //结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        //校验参数
        if(dto == null || dto.getId() == null || dto.getMsg().equals("")){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id获取对象
        ApUserRealName apUserRealName = getById(dto.getId());
        //修改参数
        apUserRealName.setStatus((short) 2);
        apUserRealName.setReason(dto.getMsg());
        //根据id修改对象
        updateById(apUserRealName);
        //返回数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核通过
     *
     * @param dto
     * @return
     */
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Autowired
    private ApUserMapper apUserMapper;
    @Override
    public ResponseResult authPass(AuthDto dto) {
        //校验参数
        if(dto == null || dto.getId() == null ){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询数据库,获取对象
        ApUserRealName apUserRealName = getById(dto.getId());
        apUserRealName.setStatus((short) 9);
        apUserRealName.setReason("审核通过");
        updateById(apUserRealName);

        //根据用户id查询出用户详情
        ApUser apUser = apUserMapper.selectById(apUserRealName.getUserId());
        //发送kafka消息
        kafkaTemplate.send("createUser-topic", JSON.toJSONString(apUser));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
